﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;

namespace WebScraper.Domain
{
    public class ScraperRequest
    {
        private string URL { get; set; }
        private string Data { get; set; }
        private int ByteSize { get; set; }
        public Stream responseStream { get; set; }
        public string Response { get; set; }
        public byte[] ResponseBinary { get; set; }

        public ScraperRequest(string URL, string Data, int ByteSize)
        {
            SetProperties(URL, Data, ByteSize);
            ExecuteRequest();
        }
            
       
        public ScraperRequest(string URL, string Data)
        {
            SetProperties(URL, Data, Convert.ToInt32(Properties.Settings.Default.BYTESIZE));
            ExecuteRequest();
        }

        private void ExecuteRequest()
        {
            WebRequest request = WebRequest.Create(URL);
            request.Timeout = Convert.ToInt32(Properties.Settings.Default.REQUESTTIMEOUT);

            if (Data.Length > 0)
            {
                request.ContentType = "application/x-www-form-urlencoded";
                request.Method = "POST";
                InsertDataInRequest(request);
            }

            GetResponseStream(request);
            RetrieveResponse();
            RetrieveBinaryResponse();
            
        }

        private void RetrieveBinaryResponse()
        {
           //
            // BinaryReader cannot handle files of large size.
            // Read the stream byte by byte.
            //
            byte[] buffer = new byte[ByteSize];
            BinaryReader binaryReader = new BinaryReader(responseStream);
            int count = 0;
            int nReturn = 0;
            while (count < buffer.Length)
            {
                nReturn = binaryReader.Read(buffer, count, buffer.Length - count);
                if (nReturn > 0)
                {
                    count += nReturn;
                }
                else
                {
                    break;
                }

            }

            binaryReader.Close();
            responseStream.Close();           

            ResponseBinary = buffer;
        }

        private void RetrieveResponse()
        {
            StreamReader reader = new StreamReader(responseStream);
            string results = reader.ReadToEnd();
            reader.Close();
            responseStream.Close();
           
            Response = results;
        }

        private void GetResponseStream(WebRequest request)
        {
            WebResponse response = request.GetResponse();
            responseStream = response.GetResponseStream();

            //response.Close();
        }

        private void InsertDataInRequest(WebRequest request)
        {
            byte[] bytes = null;
            bytes = System.Text.Encoding.ASCII.GetBytes(Data);
            request.ContentLength = bytes.Length;

            Stream outputStream = request.GetRequestStream();
            outputStream.Write(bytes, 0, bytes.Length);
            outputStream.Close();
        }

        private void SetProperties(string URL, string Data, int ByteSize)
        {
            this.URL = URL;
            this.Data = Data;
            this.ByteSize = ByteSize;
        }
    }
}